<style>
   .projects-list {
      background-color: #eee;
      padding-top: 15px;
      border: 1px solid #ddd;
      -webkit-border-radius: 4px;
      -moz-border-radius: 4px;
      border-radius: 4px;
   }
</style>
<?php

$this->headScript()
        ->appendFile($this->baseUrl('js/jquery.timeago.min.js'),'text/javascript')
        ->appendFile($this->baseUrl('js/projects/show.js'),'text/javascript');

$this->title = $this->escape($this->project['name']);
$this->headTitle($this->title);

?>
<div class="row">
   <div class="span9">
      <h4><?=$this->escape($this->project['description']) ?></h4>
      <p>
         <abbr class="timeago" title="<?= $this->project['created_at'] ?>">
            <?= date('d/m/Y', strtotime($this->project['created_at'])) ?>
         </abbr>
      </p><br/>
   </div>
   <div class="span3">
      <a href="<?=$this->url(array('controller' => 'tasks',
          'action' => 'create', 'project' => $this->project['id']),
              null, true) ?>" class="btn btn-primary btn-large pull-right">
         <i class="icon-plus icon-white"></i> New Task</a>
   </div>
</div><br/>

<?php echo $this->partial('projects/show-partial.phtml',
   array(
       'paginator' => $this->paginator,
       'term' => $this->term,
       'status_list' => $this->status_list,
       'only_status' => $this->only_status,
   ));
?>

<?php if (sizeof($this->paginator)): ?>
<br>

<div class="projects-list">
<?php
foreach($this->paginator as $registry) :
?>
<div class="row">
   <div class="frame span10 offset1" style="background-color: #fff;">
      
      <div class="row">
         <div class="span3">
            <div style="padding-top: 8px;">
               <a>#<?php echo $registry->id;?></a>
            </div>
         </div>
         <div class="span7">
            <div class="pull-right">
               <span class="frame-priority <?php echo $registry->pr_css_badge; ?>" 
                  rel="tooltip" title="Priority"><?php echo $registry->pr_description;?></span>
               <span class="frame-status <?php echo $registry->st_css_badge; ?>"
                  rel="tooltip" title="Status"><?php echo $registry->st_description;?></span>
            </div>
         </div>
      </div>
      
      <hr>
      
      <div class="row">
         <div class="span8 offset1">
            <div><?php echo $registry->description;?></div>
         </div>
      </div>
      
      <hr>
      
      <div class="row">
         <div class="span2">
            <?php
               $date = split(" ", $registry->created_at);
               $created_at = strtotime($date[0]);
            ?>
            <abbr class="timeago" title="<?= $registry->created_at ?>">
               <?= date('d/m/Y', $created_at) ?>
            </abbr>
         </div>
         <div class="span3">
            <?php // deadline
            $now = time();
            // sum 1 to not have zero days passed, so the percent always be greater than zero
            $days_passed = floor(($now - $created_at) / (60*60*24)) + 1;
            //echo $days_passed . ' ';
            
            $deadline = strtotime($registry->deadline . ' 23:59:59');
            $days_to_deadline = floor(($deadline - $created_at) / (60*60*24)) + 1;
            //echo $days_to_deadline . ' ';
            
            if ($days_to_deadline <= 0) {
               $percent = 100;
            } else if ($days_to_deadline > 0) {
               $percent = (100 * $days_passed) / $days_to_deadline;
            }
            //echo $percent;
            ?>
            <?php if ($days_to_deadline > 0): ?>
            <div><?php echo $days_to_deadline - $days_passed ?> day(s) to deadline...</div>
            <?php else:
                     if($registry->st_description != "Done"): ?>
            <div>Oh no!</div>
            <?php 
                     endif;
                  endif; ?>
            <div class="progress"
                 title="Deadline: <?php echo date('d/m/Y', strtotime($registry->deadline)); ?>">
               <div class="bar <?= $this->taskDeadline($percent, $registry->st_description) ?>"
                    aria-valuetransitiongoal="<?php echo $percent ?>"></div>
            </div>
         </div>
         <div class="span5">
            <div class="pull-right" style="margin-bottom: 10px;">
               <div class="btn-group">
                  <a href="<?=$this->url(array('controller' => 'tasks',
                        'action' => 'done',
                        'id' => $registry->id,
                        'project' => $this->project['id']), null, false)
                     ?>"
                     class="btn btn-success" rel="tooltip" title="I'm done">
                     <i class="icon-check icon-white"></i>
                  </a>
               </div>
               <div class="btn-group">
                  <a href="<?=$this->url(array('controller' => 'tasks',
                        'action' => 'update', 'id' => $registry->id,
                        'project' => $this->project['id']), null, true)
                     ?>"
                     class="btn" rel="tooltip" title="Edit">
                     <i class="icon-edit"></i>
                  </a>
                  <a href="<?=$this->url(array('controller' => 'tasks',
                      'action' => 'show',
                      'id' => $registry->id), null, true) ?>"
                     class="btn" rel="tooltip" title="Registries">
                     <i class="icon-th-list"></i> Registries
                  </a>
                  <a href="<?=$this->url(array('controller' => 'tasks',
                        'action' => 'delete',
                        'id' => $registry->id,
                        'project' => $this->project['id']), null, true) ?>"
                      class="btn btn-danger" rel="tooltip" title="Delete">
                     <i class="icon-remove icon-white"></i>
                  </a>
               </div>
            </div>
         </div>
      </div>
   </div><!-- end of .frame span10 offset1 -->
</div><!-- end of .row -->
<br>
<?php endforeach; ?>
</div>

<?php echo $this->partial('projects/show-partial.phtml',
   array(
       'paginator' => $this->paginator,
       'term' => $this->term,
       'status_list' => $this->status_list,
       'only_status' => $this->only_status,
   ));
?>

<?php else: ?>
<div class="alert">
   No tasks found.
</div>
<?php endif; ?>
